﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace NarutoClient.Pages
{
    /// <summary>
    /// Interaction logic for LoginPage.xaml
    /// </summary>
    public partial class LoginPage : UserControl
    {
        MainWindow mainWindow;

        public LoginPage()
        {
            InitializeComponent();
        }

        public LoginPage(MainWindow window)
        {
            InitializeComponent();

            this.mainWindow = window;
        }

        private void connectButton_Click(object sender, RoutedEventArgs e)
        {
            IPAddress ip;
            if (!IPAddress.TryParse(ipTextBox.Text, out ip))
            {
                MessageBox.Show("Invalid IP Adress.");
            }
            else
            {
                mainWindow.Login = loginTextBox.Text;

                try
                {
                    mainWindow.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    mainWindow.Socket.Connect(new IPEndPoint(ip, 5000));

                    mainWindow.Communication = new ClientCommunication(mainWindow, mainWindow.Socket);
                    mainWindow.Content = new CharacterPage(mainWindow);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("ERROR: Cannot connect to the Server.");
                }
            }
        }
    }
}
